package com.example.teamtribe.main.mine.setting.email;

import android.os.Bundle;
import android.util.Log;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.utils.HttpUtil;
import com.example.teamtribe.utils.URLStore;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class EmailModel {

    public void sendEmail(String token, String email, final Back back) {
        Map<String, String> params = new HashMap<>();
        params.put("email", email);
        params.put("TeamTribeToken", token);
        HttpUtil.post(URLStore.USER_EMAIL_SEND, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONObject object = new JSONObject(response.body().string());
                    Log.d("email", object.toString());
                    final int status = object.getInt("code");
                    Bundle bundle = new Bundle();
                    switch (status) {
                        case 0:
                            bundle.putString("meg", "已发送验证链接\n请检查邮箱");
                            break;
                        case 6:
                            bundle.putString("meg", "请勿频繁操作\n请稍后再试");
                            break;
                        case 8:
                            bundle.putString("meg", "邮箱已验证");
                            break;
                        case 10:
                            bundle.putString("meg", "已发送验证链接\n请检查邮箱");
                            break;
                        default:
                            back.failure(HttpUtil.getCodeMeg(status));
                            return;
                    }
                    back.success(bundle);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}